package com.phkj.qdh.ui.fragment.mynotice

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.phkj.qdh.bean.ThreeScoreBean
import com.phkj.qdh.databinding.FragmentMyThreeScoreBinding
import com.phkj.qdh.ui.adapter.usercenter.MyThreeScoreAdapter
import com.phkj.qdh.ui.fragment.user.UserVM
import com.phkj.qdh.ui.fragment.user.getUserVMProvider
import com.phkj.qdh.utils.MyItemDecoration
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import kotlinx.coroutines.flow.collectLatest

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 三能积分
 */
class MyThreeScoreFragment : Fragment() {
    private lateinit var vm: UserVM
    private var param1: ThreeScoreBean? = null
    private var param2: String? = null
    private var _binding: FragmentMyThreeScoreBinding? = null
    private val myThreeScoreAdapter by lazy { MyThreeScoreAdapter() }
    private val binding get() = _binding!!
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getParcelable(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(
            this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack()
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentMyThreeScoreBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getUserVMProvider())[UserVM::class.java]
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            tv1.text = "${param1?.currentPoints}"
            tv2.text = "${param1?.faultTolerantIntegration}"
            tv3.text = "${param1?.annualPoints}"
            srl.setOnRefreshListener {
                vm.getThreeScoreList()
            }
            rv.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = myThreeScoreAdapter
                addItemDecoration(MyItemDecoration(width = 2))
            }
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack()
            }
        }
        launchAndRepeatWithViewLifecycle {
            vm.threeScoreList.collectLatest {
                myThreeScoreAdapter.submitList(it)
            }
        }
        launchAndRepeatWithViewLifecycle {
            vm.state.collectLatest {
                binding.srl.isRefreshing = false
            }
        }
        vm.getThreeScoreList()
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: ThreeScoreBean, param2: String) = MyThreeScoreFragment().apply {
            arguments = Bundle().apply {
                putParcelable(ARG_PARAM1, param1)
                putString(ARG_PARAM2, param2)
            }
        }
    }
}